<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="clickAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="tableData" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type === 1" size="small" type="text" @click="clickAdd(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="edit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 对话框 -->
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible.sync="showDialog" width="40%" :title="form.id ? '编辑权限':'新增权限'" @close="close">
        <!-- 表单内容 -->
        <el-form ref="form" label-width="100px" :model="form" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="form.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="form.description" />
          </el-form-item>
          <el-form-item label="权限启用" prop="enVisible">
            <el-switch
              v-model="form.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="add">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAllPermissions, addPermissions, getPermissionDetail, updatePermission, delPermission } from '@/api/permisson'
import { listTotree } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      tableData: [],
      showDialog: false,
      form: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空' }],
        code: [{ required: true, message: '权限标识不能为空' }]
      }
    }
  },
  created() {
    this.getAllPermissions()
  },
  methods: {
    async getAllPermissions() {
      const res = await getAllPermissions()
      console.log(res)
      // 这里是小细节 注意哦 pid  这里所有的数据最外层只有 0

      this.tableData = listTotree(res, '0')
    },
    clickAdd(type, pid) {
      this.form.type = type
      this.form.pid = pid
      this.showDialog = true
    },
    // 添加权限
    async add() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      if (this.form.id) {
        // vuex 中可以查看是否有id
        await updatePermission(this.form)
      } else {
        await addPermissions(this.form)
      }
      this.$message.success('操作成功')
      this.showDialog = false
      this.getAllPermissions()
    },
    close() {
      this.$refs.form.resetFields()
      this.form = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },

    // 编辑
    async edit(id) {
      const res = await getPermissionDetail(id)
      this.showDialog = true
      this.form = res
    },
    // 删除
    async del(id) {
      try {
        await this.$confirm('你确定删除吗?', '温馨提示')
      } catch {
        return
      }
      await delPermission(id)
      this.$message.success('删除成功')
      this.getAllPermissions()
    }
  }
}
</script>
